package com.aye.formatconversion.service;
//
import org.springframework.stereotype.Service;
//import java.io.IOException;
//import java.nio.file.Files;
//import java.nio.file.Path;
//import java.nio.file.Paths;
//import java.util.List;
//import java.util.stream.Collectors;
//
//@Service
//public class DirectoryService {
//
//    public List<String> listDirectories(String path) {
//        try {
//            return Files.list(Paths.get(path))
//                    .filter(Files::isDirectory)
//                    .map(Path::toString)
//                    .collect(Collectors.toList());
//        } catch (IOException e) {
//            throw new RuntimeException("Failed to list directories", e);
//        }
//    }
//}


//package com.example.filedirectoryselector.service;
//
//import org.springframework.stereotype.Service;
//
//import java.io.File;
//import java.util.ArrayList;
//import java.util.List;
//
//@Service
//public class DirectoryService {
//
//    public List<File> getSubDirectories(String path) {
//        File dir = new File(path);
//        List<File> subDirectories = new ArrayList<>();
//        if (dir.exists() && dir.isDirectory()) {
//            File[] files = dir.listFiles();
//            if (files != null) {
//                for (File file : files) {
//                    if (file.isDirectory()) {
//                        subDirectories.add(file);
//                    }
//                }
//            }
//        }
//        return subDirectories;
//    }
//}

import com.aye.formatconversion.entity.DirectoryNode;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@Service
public class DirectoryService {
    public List<DirectoryNode> getSubDirectories(String path) {
        List<DirectoryNode> nodes = new ArrayList<>();
        File dir = new File(path);
        if (dir.exists() && dir.isDirectory()) {
            File[] files = dir.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.isDirectory()) {
                        boolean isLeaf = file.listFiles() == null || file.listFiles().length == 0;
                        DirectoryNode node = new DirectoryNode(file.getName(), file.getAbsolutePath(), isLeaf);
                        nodes.add(node);
                    }
                }
            }
        }
        return nodes;
    }

    public DirectoryNode getDirectoryInfo(String path) {
        File file = new File(path);
        if (file.exists() && file.isDirectory()) {
            boolean isLeaf = file.listFiles() == null || file.listFiles().length == 0;
            return new DirectoryNode(file.getName(), file.getAbsolutePath(), isLeaf);
        }
        return null;
    }
}
